<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <!-- 引入ACE插件 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
    <!-- 引入jquery CDN -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html, body {
            width: 100%;
            height: 100%;
            font-family: 'Arial', sans-serif;
        }

        body {
            background-color: #f5f5f5;
        }

        .container {
            display: flex;
            flex-direction: column;
            height: 100vh;
        }

        /* 导航栏样式 */
        .navbar {
            background-color: #2c3e50;
            padding: 0 20px;
            height: 60px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .navbar a {
            color: white;
            text-decoration: none;
            margin-right: 20px;
            font-size: 16px;
            transition: color 0.3s;
        }

        .navbar a:hover {
            color: #3498db;
        }

        .login-btn {
            background-color: #3498db;
            color: white;
            padding: 8px 15px;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .login-btn:hover {
            background-color: #2980b9;
        }

        /* 主内容区域 */
        .main-content {
            flex: 1;
            display: flex;
            padding: 20px;
            gap: 20px;
            overflow: hidden;
        }

        /* 题目描述区域 */
        .left-desc {
            width: 45%;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
            overflow-y: auto;
        }

        .left-desc h3 {
            color: #2c3e50;
            margin-bottom: 15px;
        }

        .left-desc pre {
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
            color: #333;
            background-color: #f9f9f9;
            padding: 10px;
            border-radius: 4px;
            margin-top: 15px;
        }

        /* 代码编辑区域 */
        .right-code {
            width: 55%;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
            position: relative;
            overflow: hidden; /* 防止内容超出 */
            display: flex;
            flex-direction: column;
        }

        .ace_editor {
            width: 100%;
            flex: 1;
            border-radius: 4px;
            border: none;
            overflow: hidden;
        }

        /* 底部操作区域 */
        .bottom-bar {
            height: 80px;
            background-color: white;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 -2px 5px rgba(0,0,0,0.05);
            position: relative;
        }

        .result-container {
            max-width: 60%;
            height: 100%;
            overflow-y: auto;
            border: 1px solid #e0e0e0;
            border-radius: 4px;
            padding: 10px;
        }

        .result {
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
            color: #333;
        }

        .result.error {
            color: #e74c3c;
        }

        .result.success {
            color: #2ecc71;
        }

        .btn-submit {
            width: 120px;
            height: 40px;
            font-size: 16px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .btn-submit:hover {
            background-color: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }

        .btn-submit:active {
            transform: translateY(0);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .loading {
            display: none;
            margin-right: 10px;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(0,0,0,0.1);
            border-radius: 50%;
            border-top-color: #3498db;
            animation: spin 1s ease-in-out infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        /* 添加拖动功能的样式 */
        .resizable {
            position: relative;
        }

        .resizer {
            position: absolute;
            width: 10px;
            height: 100%;
            top: 0;
            right: 0;
            cursor: ew-resize;
            background: linear-gradient(to right, transparent, #ccc, transparent);
        }

        .bottom-resizer {
            position: absolute;
            height: 10px;
            width: 100%;
            bottom: 0;
            left: 0;
            cursor: ns-resize;
            background: linear-gradient(to bottom, transparent, #ccc, transparent);
        }

        /* 滚动条样式 */
        ::-webkit-scrollbar {
            width: 8px;
            height: 8px;
        }

        ::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 4px;
        }

        ::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 4px;
        }

        ::-webkit-scrollbar-thumb:hover {
            background: #555;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 导航栏 -->
        <div class="navbar">
            <div class="nav-links">
                <a href="/">首页</a>
                <a href="/all_questions">题库</a>
                <a href="#">竞赛</a>
                <a href="#">讨论</a>
                <a href="#">求职</a>
            </div>
            <button class="login-btn">登录</button>
        </div>

        <!-- 主内容区域 -->
        <div class="main-content resizable">
            <!-- 题目描述区域 -->
            <div class="left-desc">
                <h3><span id="number">{{number}}</span>.{{title}}_{{star}}</h3>
                <pre>{{desc}}</pre>
            </div>

            <!-- 代码编辑区域 -->
            <div class="right-code">
                <pre id="code" class="ace_editor"><textarea class="ace_text-input">{{pre_code}}</textarea></pre>
            </div>

            <!-- 添加左右拖动的控制条 -->
            <div class="resizer" id="resizer"></div>
        </div>

        <!-- 底部操作区域 -->
        <div class="bottom-bar resizable">
            <div class="result-container">
                <div class="result" id="result"></div>
            </div>
            <div>
                <div class="loading" id="loading"></div>
                <button class="btn-submit" onclick="submit()">提交代码</button>
            </div>

            <!-- 添加上下拖动的控制条 -->
            <div class="bottom-resizer" id="bottom-resizer"></div>
        </div>
    </div>

    <script>
        // 初始化编辑器
        var editor = ace.edit("code");
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");
        editor.setFontSize(16);
        editor.getSession().setTabSize(4);
        editor.setReadOnly(false);
        
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true,
            showLineNumbers: true,
            showPrintMargin: false
        });

        function submit() {
            var loading = document.getElementById('loading');
            var submitBtn = document.querySelector('.btn-submit');
            var resultDiv = document.getElementById('result');
            
            // 显示加载动画，禁用按钮
            loading.style.display = 'inline-block';
            submitBtn.disabled = true;
            resultDiv.innerHTML = '';
            
            var code = editor.getSession().getValue();
            var number = document.querySelector(".left-desc h3 #number").textContent;
            var judge_url = "/judge/" + number;

            $.ajax({
                method: 'POST',
                url: judge_url,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    'code': code,
                    'input': ''
                }),
                success: function(data) {
                    loading.style.display = 'none';
                    submitBtn.disabled = false;
                    
                    if (data.status !== 0) {
                        var errorInfo = document.createElement('div');
                        errorInfo.className = 'result error';
                        errorInfo.textContent = '错误信息：';
                        resultDiv.appendChild(errorInfo);
                        
                        var errorDetails = document.createElement('div');
                        errorDetails.className = 'result error';
                        errorDetails.textContent = data.stderr || '未知错误';
                        resultDiv.appendChild(errorDetails);
                    } else {
                        var successInfo = document.createElement('div');
                        successInfo.className = 'result success';
                        successInfo.textContent = '运行结果：';
                        resultDiv.appendChild(successInfo);
                        
                        var resultDetails = document.createElement('div');
                        resultDetails.className = 'result success';
                        resultDetails.textContent = data.stdout;
                        resultDiv.appendChild(resultDetails);
                    }
                },
                error: function(xhr, status, error) {
                    loading.style.display = 'none';
                    submitBtn.disabled = false;
                    
                    var errorText = document.createElement('div');
                    errorText.className = 'result error';
                    errorText.textContent = '网络错误: 无法连接到服务器';
                    resultDiv.appendChild(errorText);
                }
            });
        }

        // 实现左右拖动功能
        document.getElementById('resizer').addEventListener('mousedown', function(e) {
            var mainContent = document.querySelector('.main-content');
            var startX = e.clientX;
            var startWidth = mainContent.offsetWidth;
            
            function handleMouseMove(e) {
                var dx = e.clientX - startX;
                var newWidth = startWidth + dx;
                newWidth = Math.max(300, Math.min(newWidth, window.innerWidth - 300));
                mainContent.style.width = newWidth + 'px';
            }
            
            function handleMouseUp() {
                document.removeEventListener('mousemove', handleMouseMove);
                document.removeEventListener('mouseup', handleMouseUp);
            }
            
            document.addEventListener('mousemove', handleMouseMove);
            document.addEventListener('mouseup', handleMouseUp);
        });

        // 实现上下拖动功能
        document.getElementById('bottom-resizer').addEventListener('mousedown', function(e) {
            var container = document.querySelector('.container');
            var bottomBar = document.querySelector('.bottom-bar');
            var startY = e.clientY;
            var startHeight = bottomBar.offsetHeight;
            
            function handleMouseMove(e) {
                var dy = e.clientY - startY;
                var newHeight = startHeight + dy;
                newHeight = Math.max(60, Math.min(newHeight, window.innerHeight - 200));
                bottomBar.style.height = newHeight + 'px';
            }
            
            function handleMouseUp() {
                document.removeEventListener('mousemove', handleMouseMove);
                document.removeEventListener('mouseup', handleMouseUp);
            }
            
            document.addEventListener('mousemove', handleMouseMove);
            document.addEventListener('mouseup', handleMouseUp);
        });
    </script>
</body>
</html>